<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 万国觉醒 展示接口
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class RokShowState extends Api
{

    public function getRules()
    {
        return array(
            'show' => array(
                'token'    => array('name' => 'token', 'require' => true, 'desc' => 'token'),
                'show_state'   => array('name' => 'show_state', 'desc' => 'show_state'),
            ),
            'show_back' => array(
                'token'    => array('name' => 'token', 'require' => true, 'desc' => 'token'),
            ),
        );
    }



    /**
     * 远征back
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function show_back()
    {
        header('Access-Control-Allow-Origin:*');
        $token = $this->token;

        //读出单条数据
        $tokeninfo = \PhalApi\DI()->notorm
            ->rok_account
            ->where('token', $token)
            ->fetchOne();

        if (  $tokeninfo  ){
            \PhalApi\DI()->notorm->rok_date_record
                ->where('token_id',$tokeninfo['id'])
                ->update(
                    array(
                        'expedition' => 1,
                    )
                );
            return $token;
        }

    }



    /**
     * rok-token
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function show()
    {
        header('Access-Control-Allow-Origin:*');
        $token = $this->token;
        $show_state  = $this->show_state;

        //数组
        $todo = array(
            '打野' => 'monster',
            '全军出击' => 'monster',
            '采集' => 'collection',
            '验证码' => 'captcha',
            '滑动解锁' => 'slide',
        );

        //读出单条数据
        $tokeninfo = \PhalApi\DI()->notorm
            ->rok_account
            ->where('token', $token)
            ->fetchOne();
        //对数据进行更新
        if ($tokeninfo) {

            \PhalApi\DI()->notorm
                ->rok_account
                ->where('id', $tokeninfo['id'])
                ->update(array(
                    'show_state' => $show_state,
                    'time' => date("Y-m-d H:i:s",time())
                ));

            if ( $todo[$show_state] ){
                //需要存入的数据
                $record = \PhalApi\DI()->notorm
                    ->rok_date_record
                    ->where(array(
                        'token_id' => $tokeninfo['id'],
                        'date' => date("Y-m-d",time())
                    ))
                    ->fetchOne();

                if( $record ){
                    \PhalApi\DI()->notorm
                        ->rok_date_record
                        ->where( 'id', $record['id']  )
                        ->update(array(
                            'note' => $tokeninfo['note'],
                            $todo[$show_state] => $record[$todo[$show_state]] + 1
                        ));

                    if ( $show_state == "滑动解锁" && $record['slide'] > 40 ) {
                        \PhalApi\DI()->notorm
                            ->rok_account
                            ->where( "id",$tokeninfo['id']  )
                            ->update(
                                array(
                                    'switch' => 1,
                                    'state' => '滑动过多,封号警告',
                                    )
                            );
                    }
                }

            }
        }

        return $record;
    }


}
